﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Sanpham_Chitiet : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["m"])))
            {
                Response.Redirect("/Online/Default/");
            }
            else
            {
                string ma = Request.QueryString["m"];
                var l = (from a in da.Sanphams where a.Masp == int.Parse(ma) select a).FirstOrDefault();
                Title = "Sản phẩm " + l.Tensp;
                lbnhan.Text = Title;
                hinhanh.ImageUrl = l.Anhto;
                hinhanh.ToolTip = l.Tensp;
                tensp.Text = l.Tensp;
                gia.Text = string.Format("{0:0,0 VNĐ}", Convert.ToInt64(l.Giaban));
                if (l.Soluong > 0) tinhtrang.Text = "Còn hàng";
                else tinhtrang.Text = "Tạm thời hết hàng";
                ttsp.Text = l.Thongtinsp;
                string[] tmp = l.TinhnangNB.Split('@');
                for (int i = 0; i < tmp.Length; i++)
                {
                    tinhnang.Text += tmp[i].ToString() + "<br />";
                }
                var mh = (from x in da.Sanphams where x.Masp.Equals(int.Parse(ma)) select x.Mahang).FirstOrDefault();
                var cl = from c in da.Sanphams where c.Mahang == mh && c.Masp != int.Parse(ma) select c;
                dlcungloai.DataSource = cl;
                dlcungloai.DataBind();

            }
        }
    }
    protected void addgiohang_Click(object sender, EventArgs e)
    {
        string ma = Request.QueryString["m"];
        var select = (from a in da.Sanphams where a.Masp.Equals(int.Parse(ma)) select new { a.Masp, a.Tensp, a.Hinhanh, a.Giaban }).FirstOrDefault();
        string m = select.Masp.ToString();
        string sp = select.Tensp;
        double gia = Convert.ToDouble(select.Giaban);
        string ha = select.Hinhanh;

        ShoppingCart cart = (ShoppingCart)Session["GioHang"];
        if (cart.ReturnID(m))
        {
            cart.Update(m, cart.Laysoluong(m,int.Parse(txtSoLuong.Text.Trim())));
            Response.Redirect("/Online/Giohang.aspx");
        }
        else
        {
            cart.AddProduct(m, sp, int.Parse(txtSoLuong.Text.Trim()), gia, ha);
            Response.Redirect("/Online/Giohang.aspx");
        }
    }
}
